#include "DACommandGraphicsTextItemProperty.h"
#include <QDebug>
//===================================================
// using DA namespace -- 禁止在头文件using！！
//===================================================

using namespace DA;

//===================================================
// DACommandGraphicsTextItemsChangeFont
//===================================================
DACommandGraphicsTextItemsChangeFont::DACommandGraphicsTextItemsChangeFont(const QList< DAStandardGraphicsTextItem* >& items,
                                                                           const QList< QFont >& newfonts,
                                                                           QUndoCommand* parent)
    : QUndoCommand(parent), m_items(items), m_newFonts(newfonts)
{
    for (const DAStandardGraphicsTextItem* i : qAsConst(items)) {
        m_oldFonts.append(i->font());
    }
}

DACommandGraphicsTextItemsChangeFont::~DACommandGraphicsTextItemsChangeFont()
{
}

void DACommandGraphicsTextItemsChangeFont::redo()
{
    QUndoCommand::redo();  //此函数会执行子内容的redo/undo
    int size = qMin(m_items.size(), m_newFonts.size());
    for (int i = 0; i < size; ++i) {
        m_items[ i ]->setFont(m_newFonts[ i ]);
    }
}

void DACommandGraphicsTextItemsChangeFont::undo()
{
    QUndoCommand::undo();
    int size = qMin(m_items.size(), m_oldFonts.size());
    for (int i = 0; i < size; ++i) {
        m_items[ i ]->setFont(m_oldFonts[ i ]);
    }
}

//===================================================
// DACommandGraphicsTextItemsChangeColor
//===================================================
DACommandGraphicsTextItemsChangeColor::DACommandGraphicsTextItemsChangeColor(const QList< DAStandardGraphicsTextItem* >& items,
                                                                             const QList< QColor >& newcolors,
                                                                             QUndoCommand* parent)
    : QUndoCommand(parent), m_items(items), m_newColors(newcolors)
{
    for (const DAStandardGraphicsTextItem* i : qAsConst(items)) {
        m_oldColors.append(i->defaultTextColor());
    }
}

DACommandGraphicsTextItemsChangeColor::~DACommandGraphicsTextItemsChangeColor()
{
}

void DACommandGraphicsTextItemsChangeColor::redo()
{
    QUndoCommand::redo();
    int size = qMin(m_items.size(), m_newColors.size());
    for (int i = 0; i < size; ++i) {
        m_items[ i ]->setDefaultTextColor(m_newColors[ i ]);
    }
}

void DACommandGraphicsTextItemsChangeColor::undo()
{
    QUndoCommand::undo();
    int size = qMin(m_items.size(), m_oldColors.size());
    for (int i = 0; i < size; ++i) {
        m_items[ i ]->setDefaultTextColor(m_oldColors[ i ]);
    }
}
